import pymysql

db = pymysql.connect(host='localhost',user='root',password='123456',port=3306,db='spiders')
cursor = db.cursor()
table = 'students'
condition = 'age > 21'
sql = 'DELETE FROM {table} WHERE {condition}'.format(table=table,condition=condition)
try:
    cursor.execute(sql)
    db.commit()
except Exception as e:
    print("Failed")
    print(e)
    db.rollback()
db.close()

'''
注意1：执行操作后，需关掉表，然后刷新再打开表
注意2：删除条件多样：运算符有大于、小于、等于、LIKE等，条件连接符有AND、OR等，故不在构造复杂的判断条件。这里直接将条件当作字符串来传递，以实现删除操作
'''